package com.ssd.demo.base.core;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import tk.mybatis.mapper.entity.Example;

import com.ssd.demo.base.exception.ServiceException;
import com.ssd.demo.base.util.DynamicUtils;
import com.ssd.demo.base.util.Log;

public abstract class BaseService<T extends BaseEntity> {

    protected Logger logger = Log.get();

    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 当前的泛型class
     */
    private Class<T> tClazz;

    /**
     * 构造方法
     */
    public BaseService() {
        tClazz = DynamicUtils.getClassActualTypeClass(getClass());
    }

    /**
     * 
     * @Title: getByFieldEqual 
     * @Description: 获取指定字段相等的一条记录
     * @author xiaoruihu
     * @param propertyName 属性名称
     * @param value  属性值
     * @return
     */
    public T getByFieldEqual(String propertyName, Object value) {
        List<T> list = getListByFieldEqual(propertyName, value);
        if (!CollectionUtils.isEmpty(list)) {
            if (list.size() > 1) {
                throw new ServiceException("返回多于一条的记录 class " + tClazz + "  propertyname [" + propertyName + "  value  " + value);
            } else {
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * @Title: getListByFieldEquel
     * @Description: 获取指定字段相等的多条条记录
     * @author xiaoruihu
     * @param propertyName 属性名
     * @param value 值
     * @return list
     */
    public List<T> getListByFieldEqual(String propertyName, Object value) {
        Example e = new Example(tClazz);
        e.createCriteria().andEqualTo(propertyName, value);
        return this.baseMapper.selectByExample(e);
    }

    /**
     * @Title: selectByExample
     * @Description: 根据查询条件返回结果集
     * @author xiaoruihu
     * @param e 查询条件
     * @return list
     */
    public List<T> selectListByExample(Example e) {
        return this.baseMapper.selectByExample(e);
    }

    /**
     * @Title: selectByExample
     * @Description: 根据查询条件返回 一个结果 如果查询出多个结果抛出异常
     * @author xiaoruihu
     * @param e 查询条件
     * @return list
     */
    public T selectByExample(Example e) {
        List<T> list = this.selectListByExample(e);
        if (!CollectionUtils.isEmpty(list)) {
            if (list.size() > 1) {
                throw new ServiceException("返回多于一条的记录selectByExample");
            } else {
                return list.get(0);
            }
        }
        return null;
    }

    public T getById(Object id) {
        return this.baseMapper.selectByPrimaryKey(id);
    }

    public List<T> getAll() {
        return this.baseMapper.selectAll();
    }

    public void insert(T t) {
        this.baseMapper.insert(t);
    }

    public void updateById(T t) {
        this.baseMapper.updateByPrimaryKey(t);
    }

    /**
     * @Title: updateByIdNotNull 
     * @Description: 只更新属性不为空的属性
     * @author xiaoruihu
     * @param t
     */
    public void updateByIdNotNull(T t){
        this.baseMapper.updateByPrimaryKeySelective(t);
    }
    
    public void deleteById(Object id) {
        this.baseMapper.deleteByPrimaryKey(id);
    }

}
